package com.gouaixin.bee.beeframework2.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ab.util.AbImageUtil;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.activity.B6_ziliaoActivity;
import com.gouaixin.bee.beeframework2.bean.MSG;
import com.gouaixin.bee.beeframework2.bean.USER;
import com.gouaixin.bee.beeframework2.extra.im.ChatData;
import com.gouaixin.bee.beeframework2.model.MemberModel;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

import java.util.List;
import java.util.Random;

import cn.bmob.v3.datatype.BmobFile;

//聊天消息适配器
public class MsgAdapter extends BaseAdapter{


	private List<MSG> lists;

    public List<MSG> getLists() {
        return lists;
    }

    public void setLists(List<MSG> lists) {
        this.lists = lists;
    }

    private Context mContext;
	private RelativeLayout layout;
    LayoutInflater inflater;

	public MsgAdapter(List<MSG> lists, Context mContext) {
		this.lists = lists;
		this.mContext = mContext;
        this.inflater= LayoutInflater.from(mContext);
	}

    public MsgAdapter( Context mContext) {
        this.mContext = mContext;
//        this.inflater= LayoutInflater.from(mContext);
    }


    String hericon="";
    String myicon="";
    ImageView icon;
    public void setIcon(String icon){
        this.hericon=icon;
    }
	
	@Override
	public int getCount() {
        if(lists!=null){
            return lists.size();
        }
            return 0;

	}

	@Override
	public Object getItem(int position) {
		return lists.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

        //如果是接受的消息
		if(lists.get(position).getFlag() == ChatData.RECEIVER){
			layout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.item_liaotian_left, null);

             icon= (ImageView) layout.findViewById(R.id.icon);
            icon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String jid=lists.get(position).getJid();
                    mLog.i("进入用户资料界面：jid=" + jid);
                    Intent it = new Intent().setClass(mContext, B6_ziliaoActivity.class);
//                it.putExtra("jid", jid);
                    int Herhight=158+new Random().nextInt(170-158+1);
                    int Herjuli=2+new Random().nextInt(30-2+1);
                    it.putExtra("Herhight",Herhight+"");
                    it.putExtra("HerId", jid);
                    it.putExtra("Herjuli",Herjuli+"KM");
                    mContext.startActivity(it);

                }
            });

            if(!hericon.equals("")){

                ImageLoader imageLoader = ImageLoader.getInstance();


                imageLoader.loadImage(hericon,new ImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String s, View view) {

                    }

                    @Override
                    public void onLoadingFailed(String s, View view, FailReason failReason) {

                    }

                    @Override
                    public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                        icon.setBackgroundDrawable(AbImageUtil.bitmapToDrawable(bitmap));

                    }

                    @Override
                    public void onLoadingCancelled(String s, View view) {

                    }
                });


            }
		}
        //如果是发送的消息
		if (lists.get(position).getFlag() == ChatData.SEND) {
			layout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.item_liaotian_right, null);

            final TextView icon= (TextView) layout.findViewById(R.id.icon);

//            icon.setBackgroundResource(R.drawable.male_one);

            MemberModel.getUSER(mContext, new MemberModel.GetUserLister() {
                @Override
                public void onSuccess(USER user) {
                    BmobFile micon = user.getIcon();
                    if (micon != null) {
                        String url = micon.getFileUrl(mContext);

                        ImageLoader.getInstance().loadImage(url, new ImageLoadingListener() {
                            @Override
                            public void onLoadingStarted(String s, View view) {

                            }

                            @Override
                            public void onLoadingFailed(String s, View view, FailReason failReason) {

                            }

                            @Override
                            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                                icon.setBackgroundDrawable(AbImageUtil.bitmapToDrawable(bitmap));
                            }

                            @Override
                            public void onLoadingCancelled(String s, View view) {

                            }
                        });
                    } else {
//                    icon.setImageResource(R.drawable.ic_user_avatar_default);
//                        icon.setBackgroundResource(R.drawable.male_one);


                        int sexType = PreferencesUtils.getInt(mContext, "sexType");
                        Log.e("test", "用户头像：icon为空，性别为(0男1女)：" + sexType);
                        switch (sexType) {
                            case 0:
                                icon.setBackgroundResource(R.drawable.male_one);
                                break;
                            case 1:
                                icon.setBackgroundResource(R.drawable.female_one);
                                break;
                        }

                    }

                }

                @Override
                public void onFailure(int i, String s) {

                }
            });



		}
        //设置显示内容
		TextView tv = (TextView) layout.findViewById(R.id.content);
		TextView time = (TextView) layout.findViewById(R.id.time);
		tv.setText(lists.get(position).getContent());
		time.setText(lists.get(position).getTime());



		return layout;
	}

}
